package online.szlib.com.core.services;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import online.szlib.com.core.domain.User;
import online.szlib.com.core.repositories.UserRepository;

@Service("userService")
public class UserServiceImpl implements UserService {
	
	@Autowired
	private UserRepository userRepository;
	
	public void addUser(User user) {
		userRepository.saveAndFlush(user);
	}

	public List<User> listUsers() {
		return userRepository.findAll();
	}
	
	public User getUser(long userid){
		return userRepository.findOne(userid);
	}
	
	public void deleteUser(User user){
		userRepository.delete(user);
	}
	
	public Long create(User user){
		userRepository.saveAndFlush(user);
		return 1L;
	}
	
	public void update(User user) {
//		User temp = userRepository.findOne(5L);
//		temp.setUserName(user.getUserName());
		userRepository.saveAndFlush(user);
		System.out.println("Udate one user:" + user);
	}
	
	public void deleteById(Long id) {
		User user = userRepository.findOne(id);
		userRepository.delete(user);
	}
	
	public User login(String userName, String password){
		return userRepository.findByUserName(userName, password).get(0);
	}
}
